<template>
  <div class="AddOrUpdate_main add_container tenantMenu_main main_container user_container">
    <div v-loading="loading" class="main_main">
      <div class="main_main_container">
        <div class="main_main_left">
          <div class="title" />
          <!-- <el-input
            v-model="filterText"
            clearable
            style="margin-bottom:5px"

          /> -->
          <div class="main_main_search_input">
            <el-input
              v-model="filterText"
              style="width:195px"
              clearable
              placeholder="请输入备件类型"
              @keyup.enter.native="secrchTreeNode"
              @clear="secrchTreeNode"
            />
            <el-button style="background:#387bfc;width:30px;" icon="el-icon-search" @click.stop="secrchTreeNode" />
          </div>
          <div class="main_main_left_top">
            <el-tree
              ref="tree"
              node-key="id"
              :current-node-key="typeId"
              :data="typeListArr"
              :expand-on-click-node="false"
              :default-expand-all="true"
              :highlight-current="true"
              :props="defaultProps"
              :filter-node-method="filterNode"
              @node-click="getSpareInfo"
            />
          </div>
        </div>
        <div class="main_main_right">
          <div class="SearchBar_main main_main_search">
            <div class="main_main_search_item">
              <el-input v-model="pageData.searchValue" class="search_input" placeholder="请输入备件编码或名称" clearable @keyup.enter.native="()=>{pageData.pageNum=1; getList()}" @clear="getList" />
              <el-button class="button_icon" icon="el-icon-search" @click.stop="()=>{pageData.pageNum=1; getList()}" />
            </div>
          </div>
          <div ref="table_container" class="table_container">
            <el-table
              ref="table"
              :data="tableData"
              stripe
              border
              :height="340"
              style="width: 99.9%"
              :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
              @sort-change="
                (column) => {
                  sortChange(column, this);
                }
              "
              @selection-change="
                (val) => {
                  selectionChange(val, this);
                }
              "
            >
              <el-table-column type="selection" width="70" align="center" />
              <Column
                v-for="(column, index) in defaultTableColumns"
                :key="index"
                :prop="column.prop"
                :is-show="column.isShow"
                :label="column.label"
                :sortable="column.sortable"
                :width="column.width"
                :show-overflow-tooltip="true"
              />
            </el-table>
          </div>

          <div class="addtype-main_bottoms">
            <pagination
              :total="total"
              :page.sync="pageData.pageNum"
              :limit.sync="pageData.pageSize"
              :selected-num="selectionsAll&&selectionsAll.length"
              @pagination="getList"
            />
          </div>
        </div>
      </div>
    </div>
    <div style="background:white;border-top:1px solid #ECECEC;" class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">确定</el-button>
    </div>
  </div>
</template>

<script>
import { getAllBatchInventory } from '@/api/equipmentManage/sparePart/outWarehouse'
import { list as typeList } from '@/api/equipmentManage/sparePart/type'
import AddOrUpdate from '../sparePartsInfo/addOrUpdate.vue' // 新增或编辑页面
export default {
  name: 'Index',
  props: {
    layerid: {},
    selectedRow: {}
  },
  data() {
    return {
      filterText: '', // 树节点搜索字段
      loading: false,
      pageId: '备件信息',
      pageData: {
        isAsc: 'DESC',
        orderByColumn: 'updateTime',
        pageNum: 1,
        pageSize: 20,
        searchValue: ''
      },
      stockData: {
        searchBatch: ''
      },
      materialTableData: {
        isAsc: 'desc',
        orderByColumn: 'updateTime',
        pageNum: 1,
        pageSize: 20,
        searchValue: ''
      },
      stockList: [],
      total: 1, // 数据一共有多少条
      mainId: 'batchInventoryId',
      tableHeight: '0',
      tableData: [],
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'spareCode', label: '备件编码' },
        { prop: 'spareName', label: '备件名称' },
        { prop: 'spareModel', label: '规格型号' },
        { prop: 'supplierName', label: '备件供应商' },
        { prop: 'spareTypeName', label: '备件类型' },
        { prop: 'numberUnitLabel', label: '备件单位' },
        { prop: 'safeInventory', label: '安全库存' },
        { prop: 'batch', label: '批号' },
        { prop: 'number', label: '库存数量' },
        { prop: 'deviceSpareWarehouseName', label: '仓库' }
      ],
      materialData: [],
      rowdDeviceSpareStandingBookId: '',
      rowDeviceSpareWarehouseId: '',
      typeListArr: [],
      rowId: '',
      typeId: '',
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  // watch: {
  //   filterText(val) {
  //     this.$refs.tree.filter(val)
  //   }
  // },
  mounted() {
    this.getList()
    this.selectionsAll = this.selectedRow
    this.getTypeList()
  },
  methods: {
    secrchTreeNode() {
      this.$refs.tree.filter(this.filterText)
    },
    // 树节点搜索
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    getSpareInfo(item) {
      this.rowId = item.id
      // this.selections = []
      // this.selectionsAll = []
      this.typeId = item.id
      this.getList()
    },
    getTypeList() {
      typeList(this.pageData).then(res => {
        this.typeListArr.push({ name: '备件信息', id: '', children: [] })
        this.typeListArr[0].children = res.data
        this.getSpareInfo({ name: '备件信息', id: '', children: [] })
      })
    },
    getList() {
      getAllBatchInventory({ ...this.pageData, spareType: this.typeId, status: 1 }).then(res => {
        res.data.filter(item => {
          this.selectionsAll.filter(ele => {
            if (item.id == ele.id) {
              item.useNumber = ele.useNumber
              item.outNumber = ele.outNumber
            }
          })
        })
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
        })
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
          }// props
        },
        area: ['880px', '570px'],
        title: '新增备件', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    },
    onSubmit() {
      this.$layer.close(this.layerid)// 关闭弹框
      this.$parent.addMaterial(this.selectionsAll)
    },
    chooseMaterial(e) {
      this.materialData = e
    }
  }
}
</script>

<style lang="less" scoped>
.main_main_search_input{
  margin-top: 20px;
  margin-bottom: 10px ;
 /deep/ .el-input__inner{
    height: 30px;
  }
}
/deep/.el-input__suffix {
  height: 30px !important;
}
.main_main {
  height: calc(100% - 50px);
}
.main_main_container {
  width: 100%;
  height: 100%;
  display: flex;

  .main_main_left {
    width: 250px;
    height: calc(100% - 20px);
    padding-right: 20px;
    .title {
      // padding: 10px 0;
      // border-bottom: 1px solid #387BFC;
      // margin-bottom: 10px;
      margin: 15px 0;
    }
    .main_main_left_top {
      width: 100%;
      height: calc(100% - 80px);
      overflow-y: auto;
      .el-tree {
        background-color: transparent;
      }
      /deep/ .el-tree-node__content:hover {
        background-color: rgba(56, 123, 252, 0.5) !important;
        color: white;
      }
    }
    .main_main_left_bottom {
      width: 100%;
      .main_main_left_bottom_item {
        background-color: rgba(74, 77, 82, 0.2);
        height: 40px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding-left: 24px;
      }
      .main_main_left_bottom_item:last-child {
        border-top: 1px solid white;
      }
      .main_main_left_bottom_item:hover {
        background-color: rgba(56, 123, 252, 0.5);
        color: white;
      }
      .active {
        .main_main_left_bottom_item {
          background-color: rgba(56, 123, 252, 1);
          color: white;
        }
      }
    }
  }
  .main_main_right {
    width: calc(100% - 250px);
    height: 100%;
    display: flex;
    flex-direction: column;
    border-left: 1px solid #ECECEC;

  }
}
.button{
    width: 100px;
    height: 30px;
    color: #fff;
    background-color: #387bfc;
    border-radius: 4px;
    text-align: center;
    line-height: 30px;
}
.formItem_header{
  justify-content: flex-start !important;
  align-items: flex-start !important;
  height: 40px !important;
}
.formItem_header_operate_items{
  height: 30px;
  display: flex;justify-content: center;align-items: center;
  padding: 0px 10px;font-size: 16px;border-radius: 4px;
  color: rgba(56, 123, 252, 1);background-color: rgba(235, 242, 255, 1);
  .item_name{
    font-size: 14px;padding-left: 10px;
  }
}
.formItem_header_operate_items:hover{
  background-color: rgba(235, 242, 255, 0.5);
}
/deep/ .el-table {
  height: 100% !important;
}
.table_container{
  margin-top: 20px !important;
}
.addtype-main_bottoms{
  padding-right: 20px !important;
  margin: 10px 0;
}
.main_search_item{
  padding-right: 20px !important;
}
.main_main_search{
  padding-right: 20px !important;
}
.main_main_search_item{
  padding-left: 20px !important;
}
.main_main{
  margin-right:0 !important;
  padding-top:0 !important;
}
.button_icon{
 background:#387bfc;
 width:30px;
}
.search_input{
  width:200px;
  margin-right:10px
}
</style>
